$(function () {
    function getList() {
        $.ajax({
            url: '/admin/category/list',
            success(res) {
                let list = res.data
                let html = template('temp', {list})
                $('.category_table tbody').html(html)
            }
        })
    }
    getList()

    //新增分类  
    $('.add-save').click(function () {
        let name = $('#kindName').val().trim()
        let slug = $('#kindSlug').val().trim()
        layer.msg('恭喜你新增成功');
        $('#kindName').val('')
        $('#kindSlug').val('')
        $.ajax({
            url: '/admin/category/add',
            type: 'post',
            data: { name, slug },
            success(res) {
                $('#myModal').modal('hide')
                getList()
            }
        })
    })

    //删除功能
    $('.category_table tbody').on('click', '.delete', function () {
        id = $(this).data('id')
        layer.confirm('您确定要删除嘛', function(index){
            layer.close(index);
            $.ajax({
            url: '/admin/category/delete',
            type: 'post',
            data: { id },
            success(res) {
                layer.msg('恭喜你删除成功');
                getList()
            }
        })
            });
        
    })

    //编辑功能
    let item
    $('.category_table tbody').on('click', '.edit', function () {
        $('#editModal').modal('show')
        item = $(this).data('item')
        // console.log(item)
        $('.edit-name').val(item.name)
        $('.edit-slug').val(item.slug)
     })

    $('.edit-save').click(function () {
        layer.msg('恭喜你修改成功');
        let parm = {
            id: item.id,
            name: $('.edit-name').val(),
            slug:$('.edit-slug').val()
        }
        console.log(parm)
        $.ajax({
            url: '/admin/category/edit',
            type: 'post',
            data: parm,
            success(res) {
                console.log(res)
                $('#editModal').modal('hide')
                getList()
            }
        })
    })
})